﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GameLogic.Controls;

namespace TestUi.Content
{
	public partial class GameContainerContent : UserControl, IGameFeatures
	{
		public GameContainerContent()
		{
			// Required to initialize variables
			InitializeComponent();
		}

        public event EventHandler<ScoresChangedEventArgs> ScoresChanged;
        public event EventHandler<LivesChangedEventArgs> LivesChanged;
        public event EventHandler<TimeChangedEventArgs> TimeChanged;
        //public event EventHandler<HighscoreEntryArgs> HighscoreEntryFired;
        public event EventHandler PauseTimerFired;
        public event EventHandler ResumeTimerFired;
        public event EventHandler<GameOverEventArgs> GameOver;
        public event EventHandler NewGameFired;

        public void Start()
        {
            _continueButton.Visibility = Visibility.Collapsed;
            this.WriteToStatus("Game started.");
        }

        public void Stop()
        {
            this.WriteToStatus("Game stopped.");
        }

        public void Pause()
        {
            this.WriteToStatus("Game paused.");
        }

        public void Resume()
        {
            this.WriteToStatus("Game resumed.");
        }

        //public void GameOver(GameOverEventArgs args)
        //{
        //     // Do something useful with the gameover info and give the player time to read it
        //    this.WriteToStatus(String.Format(
        //        "{0}! Reason: {1}\nYou made it to rank {2} of the highscore list",
        //        args.GameOverState, 
        //        "asdasda",
        //        args.HighscoreRank));

        //    _continueButton.Visibility = Visibility.Visible;

        //    // Fire game over event back to game container
        //    if (this.GameOve != null)
        //    {
        //        GameOve(this, EventArgs.Empty);
        //    }

        //    //// Add player to the highscore list
        //    //if (this.HighscoreEntryFired != null)
        //    //{
        //    //    HighscoreEntryArgs highscoreArgs = new HighscoreEntryArgs();
        //    //    highscoreArgs.Player = "TestPlayer";
        //    //    HighscoreEntryFired(this, highscoreArgs);
        //    //}
        //}

        //
        // Button Events
        //
        private void _incScores_Click(object sender, RoutedEventArgs e)
        {
            ChangeScores(50);
        }

        public void ChangeScores(int p)
        {
            if (ScoresChanged != null)
            {
                ScoresChangedEventArgs args = new ScoresChangedEventArgs();
                args.Scores = p;
                ScoresChanged(this, args);
            }
        }

        private void _decScores_Click(object sender, RoutedEventArgs e)
        {
            ChangeScores(-50);
        }

        private void _incLives_Click(object sender, RoutedEventArgs e)
        {
            ChangeLives(+1);
        }

        public void ChangeLives(int p)
        {
            if (LivesChanged != null)
            {
                LivesChangedEventArgs args = new LivesChangedEventArgs();
                args.Lives = p;
                LivesChanged(this, args);
            }
        }

        private void _decLives_Click(object sender, RoutedEventArgs e)
        {
            ChangeLives(-1);
        }

        private void _continueButton_Click(object sender, RoutedEventArgs e)
        {
            // Start new game
            if (NewGameFired != null)
            {
                NewGameFired(this, EventArgs.Empty);
            }

            _continueButton.Visibility = Visibility.Collapsed;
        }

        void WriteToStatus(string s)
        {
            _status.Text = String.Format("{0}\n{1}", s, _status.Text);
        }


        public bool IsTimeBased
        {
            get { return true; }
        }

        public bool IsLifeBased
        {
            get { return true; }
        }

        public string GameName
        {
            get { return "Test UI"; }
        }



        public TimeSpan GameDuration
        {
            get { throw new NotImplementedException(); }
        }


        public string DeveloperName
        {
            get { throw new NotImplementedException(); }
        }

        public string CompanyName
        {
            get { throw new NotImplementedException(); }
        }

        public string Description
        {
            get { throw new NotImplementedException(); }
        }

        public Uri SettingsPage
        {
            get { return null; }
        }


        public void OnScoresChanged(int scores)
        {
        }

        public void OnLivesChanged(int lives)
        {
        }
    }
}